package cn.skyquant.quant4j.service.boot.web.controller;

import cn.skyquant.quant4j.api.dto.ResultDTO;
import cn.skyquant.quant4j.api.enums.BorkerType;
import cn.skyquant.quant4j.api.enums.ForexConfigType;
import cn.skyquant.quant4j.api.forex.ForexConfigDTO;
import cn.skyquant.quant4j.api.forex.ForexConfigSeriesDTO;
import cn.skyquant.quant4j.api.forex.ForexConfigSymbolDTO;
import cn.skyquant.quant4j.api.forex.ForexService;
import cn.skyquant.quant4j.sdk.enums.TradeDirection;
import cn.skyquant.quant4j.service.boot.model.authority.RoleAuthority;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/forex")
@RoleAuthority(roleNames = {RoleAuthority.administrator,RoleAuthority.client})
public class ForexController {

    @Autowired
    ForexService forexService;
    /**配置项管理**/
    @RequestMapping(value = "/config",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO<Void> add(
            @RequestParam(value = "name",required = true) String name,
            @RequestParam(value = "borker_type",required = true) BorkerType borkerType,
            @RequestParam(value = "type",required = true)  ForexConfigType type) {
        return forexService.addConfig(name,borkerType,type);
    }

    @RequestMapping(value = "/config/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public ResultDTO<Void> delete(@PathVariable Integer id){
        return forexService.deleteConfig(id);
    }

    @RequestMapping(value = "/config/{id}",method = RequestMethod.PUT)
    @ResponseBody
    public ResultDTO<Void> update(
            @PathVariable Integer id,
            @RequestParam(value = "name",required = true) String name) {
        return forexService.updateConfig(id,name);
    }

    @RequestMapping(value = "/config/{id}/copy",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO<Void> copy(
            @PathVariable Integer id,
            @RequestParam(value = "name",required = true) String name){
        return forexService.copyConfig(id,name);
    }

    @RequestMapping(value = "/config/{id}",method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO<ForexConfigDTO> get(@PathVariable Integer id) {
        return forexService.getConfigById(id);
    }

    @RequestMapping(value = "/config",method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO<List<ForexConfigDTO>> list(
            @RequestParam(value = "type",required = true)  ForexConfigType type,
            @RequestParam(value = "borker_type",required = true) BorkerType borkerType) {
        return forexService.findConfigByTypeAndBorkerType(type,borkerType);
    }

    /**货币组管理**/
    @RequestMapping(value = "/config/symbol",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO<Void> updateConfigSymbol(
            @RequestParam(value = "configId",required = true) Integer configId,
            @RequestParam(value = "str",required = true)String str){
        if (StringUtils.isEmpty(str) || configId == null) {
            return ResultDTO.error1;
        }
        String[] s0 = str.split(";");
        if (s0.length == 0) {
            return ResultDTO.error1;
        }
        List<ForexConfigSymbolDTO> insertList = Lists.newArrayList();
        int i=0;
        for (String sp0 : s0) {
            if (StringUtils.isEmpty(sp0) || sp0.trim().length() == 0) {
                continue;
            }
            String[] sp1 = sp0.split("_");
            if (sp1.length != 3) {
                continue;
            }
            boolean cancontinue = true;
            for(int k=0;k<sp1.length;k++){
                if(sp1[k]==null || sp1[k].trim().length()==0){
                    cancontinue = false;
                    break;
                }
            }
            if(!cancontinue){
                continue;
            }
            ForexConfigSymbolDTO configSymbolDTO = new ForexConfigSymbolDTO();
            configSymbolDTO.symbol = sp1[0].trim();
            configSymbolDTO.tradeDirection = TradeDirection.parse(sp1[1].trim());
            configSymbolDTO.standardPosition = Integer.parseInt(sp1[2].trim());
            configSymbolDTO.i = i++;
            insertList.add(configSymbolDTO);
        }
        return forexService.updateConfigSymbol(configId,insertList);
    }

    @RequestMapping(value = "/config/symbol",method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO<List<ForexConfigSymbolDTO>> findConfigSymbol(
            @RequestParam(value = "configId",required = true) Integer configId){
        return forexService.findConfigSymbol(configId);
    }


    @RequestMapping(value = "/config/series",method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO<List<ForexConfigSeriesDTO>> findConfigSeries(
            @RequestParam(value = "configId",required = true) Integer configId){
        return forexService.findConfigSeries(configId);
    }

    /**加仓数列管理**/
    @RequestMapping(value = "/config/series",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO<Void> updateConfigSeries(
            @RequestParam(value = "configId",required = true) Integer configId,
            @RequestParam(value = "str",required = true)String str){
        if (StringUtils.isEmpty(str) || configId == null) {
            return ResultDTO.error1;
        }
        String[] s0 = str.split(";");
        if (s0.length == 0) {
            return ResultDTO.error1;
        }
        List<ForexConfigSeriesDTO> insertList = Lists.newArrayList();
        int i=0;
        for (String sp0 : s0) {
            if (StringUtils.isEmpty(sp0) || sp0.trim().length() == 0) {
                continue;
            }
            String[] sp1 = sp0.split("_");
            if (sp1.length != 6) {
                continue;
            }
            boolean cancontinue = true;
            for(int k=0;k<sp1.length;k++){
                if(sp1[k]==null || sp1[k].trim().length()==0){
                    cancontinue = false;
                    break;
                }
            }
            if(!cancontinue){
                continue;
            }
            ForexConfigSeriesDTO configSeriesDTO = new ForexConfigSeriesDTO();
            configSeriesDTO.sl = Integer.parseInt(sp1[0].trim());
            configSeriesDTO.tp = Integer.parseInt(sp1[1].trim());
            configSeriesDTO.addpip = Integer.parseInt(sp1[2].trim());
            configSeriesDTO.backpip = Integer.parseInt(sp1[3].trim());
            configSeriesDTO.prs = Integer.parseInt(sp1[4].trim());
            configSeriesDTO.time = Integer.parseInt(sp1[5].trim());
            configSeriesDTO.i = i++;
            insertList.add(configSeriesDTO);
        }
        return forexService.updateConfigSeries(configId,insertList);
    }

}
